<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>YK Value-added System</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/style.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/admin.css" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/common.css" />
	<script src="<%=basePath%>js/sys/validate.js" type="text/javascript"></script>
	<script src="<%=basePath%>js/json.js" type="text/javascript"></script>
	<script src="<%=basePath%>js/prototype.js" type="text/javascript"></script>
	<script type="text/javascript" charset="utf-8" defer="defer" src="<%=basePath%>js/wdatepicker/WdatePicker.js"></script>
	<script type="text/javascript">
		var judgment;
		function check(){
			if(saveApp()){
				return true;
			}else{
				return false;
			}
		}

		function getRTData(n,value){//根据选择的权限类型带出相应的数据
			var strs=value.split(";");
			var id=document.getElementById("id").value;
			var url = "<%=basePath%>admin/system/rightsMgmt/role!getRTData.action?id="+id+"&table_name="+strs[1]+"&rightTypeID="+strs[0];
           	new Ajax.Request(url,{method:'post',
           		onComplete:function(json){
	           		var rtDataStr=json.responseText.parseJSON().rtDataStr;
	            	var datas=rtDataStr.split(";");
	            	var objs=document.getElementById("rtdata"+n).options;

	            	objs.length=0;
	            	for(var i=0;i<datas.length;i++){
	            		var m=datas[i].split(":");
	            		var obj=new Option(m[1],m[0]+";"+strs[0]);
	            		objs.add(obj);
	            	}
	            	document.getElementById("roleRTData"+n).options.length=0;
	           	}
	        });
		}

		function addRTData(n){//添加用户
			var obj2=document.getElementById("rtdata"+n).options;//左
			var obj=document.getElementById("roleRTData"+n).options;//右
			
			var w=true;
			var value="";
			for(var i=0;i<obj.length;i++){
				if(document.getElementById("roleRTData"+n).options[i].value==document.getElementById("rtdata"+n).options[obj2.selectedIndex].value){
					w=false;
				}
			}
			if(w){
				var aboj=new Option(document.getElementById("rtdata"+n).options[obj2.selectedIndex].text,document.getElementById("rtdata"+n).options[obj2.selectedIndex].value);
				document.getElementById("roleRTData"+n).options.add(aboj);
			}
		}
		
		function deleteRoleRTData(n){//删除用户
			if(document.getElementById("roleRTData"+n).options.selectedIndex=="-1"){
				alert("请选中右选框中的用户信息！");
				return;
			}
			document.getElementById("roleRTData"+n).removeChild(document.getElementById("roleRTData"+n).options[document.getElementById("roleRTData"+n).options.selectedIndex]);
		}
		function addRow(){
			
			var newRow = tabActive.insertRow();
			var rowIndex=tabActive.rows.length-2;
			//var cell1 = newRow.insertCell();
			//cell1.innerText = '' + rowIndex;
			
			var cell1 = newRow.insertCell();
			cell1.align="center";
			cell1.innerHTML ="<input type='checkBox' id='num" + rowIndex + "' name='num' value='" + rowIndex + "'/>";
			
			var cell1 = newRow.insertCell();
			cell1.align="center";
			cell1.innerHTML="<select name='rightType' id='rightType" + rowIndex + "' style='width: 180px;height:100px' multiple='multiple' ondblclick='getRTData(" + rowIndex + ",this.options[this.selectedIndex].value);'><c:forEach var='rightType' items='${rightTypeList}'><option value='${rightType.id};${rightType.table_name}'>${rightType.name}</option></c:forEach></select>";

			var cell1 = newRow.insertCell();
			cell1.align="center";
			cell1.innerHTML ="<select name='rtdata' id='rtdata" + rowIndex + "' style='width: 180px;height:100px' multiple='multiple' ondblclick='addRTData(" + rowIndex + ");'>";
				+"</select>";
				
			var cell1 = newRow.insertCell();
			cell1.align="center";
			cell1.innerHTML ="<select name='roleRTData' id='roleRTData" + rowIndex + "' style='width: 180px;height:100px' multiple='multiple' ondblclick='deleteRoleRTData(" + rowIndex + ")'></select>";
		}

		function saveApp(){
			var Items = "";
			var iNext;
			var rightType=document.getElementsByName("rightType");
			for(var i=0;i<rightType.length-1;i++){
				for(var j=i+1;j<rightType.length;j++){
					if(rightType[i].value==rightType[j].value){
						alert("权限类型不能相同！");
						return false;
					}
				}
			}
			//计划
			for(var i=0;i<tabActive.rows.length-2;i++){
				//每一行之间用※分隔，每一个字段之间用`分隔
				if(Items != ""){
					Items += "※";
				}
				iNext = i+1;
				var value='';
				for(var j=0;j<document.getElementById("roleRTData"+iNext).options.length;j++){
					if(value!=''){
						value=value+"`"+document.getElementById("roleRTData"+iNext).options[j].value;
					}else{
						value=document.getElementById("roleRTData"+iNext).options[j].value;
					}
				}
				Items+=value;
			}
			document.getElementById("itemsVal").value = Items;
			return true;
		}

		function delRow(){
			if(window.confirm("确定删除吗?")){
				//删除最后一行
				if(tabActive.rows.length<=2){
					alert("没有记录可删除！");
					return;
				}
				var nums=document.getElementsByName("num");
				var temp=true;
				for(var i=nums.length-1;i>=0;i--){
					if(document.getElementById("num"+(i+1)).checked){
						tabActive.deleteRow(i+2);
						temp=false;
					}
				}
				if(temp){
					alert("请选择要删除的记录！");
				}else{
					//重新赋值ID
					var num=document.getElementsByName("num");
					var rightType=document.getElementsByName("rightType");
					var rtdata=document.getElementsByName("rtdata");
					var roleRTData=document.getElementsByName("roleRTData");
					for(var i=0;i<num.length;i++){
						num[i].id="num"+(i+1);
						rightType[i].id="rightType"+(i+1);
						rtdata[i].id="rtdata"+(i+1);
						roleRTData[i].id="roleRTData"+(i+1);
					}
				}
			}
		}
	</script>
  </head>
  
  <body>
<table width="95%" border="0" align="center" cellspacing="0">
  <tr>
    <td height="30"><table width="100%" border="0">
      <tr>
          <td colspan="2"><hr width="56%" align="left"></td>
		</tr>
    </table></td>
  </tr>
  <tr>
    <td><form id="form1" name="form1" method="post" action="<%=basePath%>admin/system/rightsMgmt/role!editRoleRithtType.action" onsubmit="return check();">
     
      <table width="70%" border="0" cellspacing="3">
      <tr>
          <td colspan="2"><font color="red" style="font-size:12px;"></font></td>
         <td>
          <font color="red" style="font-size:12px;"><s:actionmessage/><s:actionerror/></font>
          </td>
		</tr>
		<tr>
          <td colspan="2" align="left">
           <div style="overflow:auto;width:610px;height:450px">
       		<table border="1" align="left"  
          		class=TableList style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 98%; BORDER-RIGHT-WIDTH: 0px"  id="tabActive" name="tabActive" >
          		<tr>
          			<td colspan="4" align="right">
          				<strong>角色权限类型对应关系</strong>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
          				<a  onclick="addRow()" >新增</a>
          					&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
          				<a  onclick="delRow()"  >删除</a>
          				<input type="hidden" id="itemsVal" name="itemsVal" value="${itemsVal}"/>
						<input type="hidden" id="id" name="id" value="${role.id}"/>
          			</td>
          		</tr>
          		<tr>
          			<th align="center" width="5%">选择</th>
          			<th align="center" width="30%">权限类型列表</th>
          			<th align="center" width="30%">权限类型对应的数据</th>
          			<th align="center" width="35%">已选的权限类型数据</th>
          		</tr>
          		<c:forEach var="rt" items="${roleRightTypeList}" varStatus="tag">
					<tr>
						<td align="center">
							<input type="checkBox" id="num${tag.count}" name="num" value="<s:property value='#st.count'/>"/>
						</td>
						<td align="center">
							<select name="rightType" id="rightType${tag.count}" style="width: 180px;height:100px" multiple="multiple" ondblclick="getRTData(${tag.count},this.options[this.selectedIndex].value);">
				          		<c:forEach var="rt2" items="${rightTypeList}">
				          			<option value="${rt2.id};${rt2.table_name}" <c:if test="${rt2.id==rt.id}">selected</c:if>>${rt2.name}</option>
				          		</c:forEach>
							</select>
						</td>
						<td align="center">
							<select name="rtdata" id="rtdata${tag.count}" style="width: 180px;height:100px" multiple="multiple" ondblclick="addRTData(${tag.count})">
								<c:forEach var="r1" items="${rt.allList}" varStatus="tag1">
				          			<option value="${r1.dataID};${rt.id}">${r1.dataName}</option>
				          		</c:forEach>
							</select>
						</td>
						<td align="center">
							<select name="roleRTData" id="roleRTData${tag.count}" style="width: 180px;height:100px" multiple="multiple" ondblclick="deleteRoleRTData(${tag.count})">
								<c:forEach var="r2" items="${rt.list}" varStatus="tag2">
				          			<option value="${r2.dataID};${r2.right_type_id}">${r2.dataName}</option>
				          		</c:forEach>
							</select>
						</td>
		       		</tr>
		        </c:forEach>
          	</table>
          	</div> 
          </td>
		</tr>
		<tr>
          <td colspan="3"><hr width="82%" align="left"></td>
		</tr>
        <tr>
          <td align="right"><input type="submit" name="Submit" value="修改" class="Button" /></td>
          <td colspan="1" align="left"><input type="button" name="Submit22" value="返回" class="Button"  onclick="history.back();"/></td>
        </tr>
      </table>
        </form>
    </td>
  </tr>
</table>
</body>
</html>